package com.cmict.data.infra.controller.admin.message;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.excel.core.util.ExcelUtils;
import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import com.cmict.data.infra.api.message.dto.MessageDTO;
import com.cmict.data.infra.controller.admin.vo.*;
import com.cmict.data.infra.convert.message.MessageTemplateConvert;
import com.cmict.data.infra.dal.dataobject.message.MessageTemplateDO;
import com.cmict.data.infra.service.message.MessageTemplateService;
import com.cmict.data.system.api.conf.dto.ConfSystemDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 消息模板")
@RestController
@RequestMapping("/notify/message-template")
@Validated
public class MessageTemplateController {

    @Resource
    private MessageTemplateService messageTemplateService;

    @PostMapping("/create")
    @ApiOperation("创建信息模板")
    @PreAuthorize("@ss.hasPermission('sys:message-template:create')")
    public CommonResult<Long> createMessageTemplate(@Valid @RequestBody MessageTemplateCreateReqVO createReqVO) {
        return success(messageTemplateService.createMessageTemplate(createReqVO));
    }

    @PostMapping("/testEmail")
    @ApiOperation("测试邮箱")
    public CommonResult<Boolean> testEmail(@RequestBody ConfSystemDTO confSystemDTO) {
        return success(messageTemplateService.testEmail(confSystemDTO));
    }

    @PutMapping("/update")
    @ApiOperation("更新信息模板")
    @PreAuthorize("@ss.hasPermission('sys:message-template:update')")
    public CommonResult<Boolean> updateMessageTemplate(@Valid @RequestBody MessageTemplateUpdateReqVO updateReqVO) {
        messageTemplateService.updateMessageTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除信息模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:message-template:delete')")
    public CommonResult<Boolean> deleteMessageTemplate(@RequestParam("id") Long id) {
        messageTemplateService.deleteMessageTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得信息模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:message-template:query')")
    public CommonResult<MessageTemplateRespVO> getMessageTemplate(@RequestParam("id") Long id) {
        MessageTemplateDO messageTemplate = messageTemplateService.getMessageTemplate(id);
        return success(MessageTemplateConvert.INSTANCE.convert(messageTemplate));
    }

    @GetMapping("/list")
    @ApiOperation("获得信息模板列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:message-template:query')")
    public CommonResult<List<MessageTemplateRespVO>> getMessageTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<MessageTemplateDO> list = messageTemplateService.getMessageTemplateList(ids);
        return success(MessageTemplateConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得信息模板分页")
    @PreAuthorize("@ss.hasPermission('sys:message-template:query')")
    public CommonResult<PageResult<MessageTemplateRespVO>> getMessageTemplatePage(@Valid @RequestBody MessageTemplatePageReqVO pageVO) {
        PageResult<MessageTemplateDO> pageResult = messageTemplateService.getMessageTemplatePage(pageVO);
        return success(MessageTemplateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出信息模板 Excel")
    @PreAuthorize("@ss.hasPermission('sys:message-template:export')")
    @OperateLog(type = EXPORT)
    public void exportMessageTemplateExcel(@Valid MessageTemplateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MessageTemplateDO> list = messageTemplateService.getMessageTemplateList(exportReqVO);
        // 导出 Excel
        List<MessageTemplateExcelVO> datas = MessageTemplateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "信息模板.xls", "数据", MessageTemplateExcelVO.class, datas);
    }

    @PostMapping("/sendMessage")
    @ApiOperation("流程调用")
    @PreAuthorize("@ss.hasPermission('sys:message-template:create')")
    public CommonResult<Boolean> createMessageTemplate(@Valid @RequestBody MessageDTO createReqVO) {
        return success(messageTemplateService.sendMessageByTemplate(createReqVO));
    }

}
